package com.example.mapper;

import com.example.domain.Check;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.*;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.List;

@Mapper
public interface CheckMapper {

    @Select("select * from [dbo].[Check]")
    List<Check> showAllCheck();

    @Select("select * from [dbo].[Check] where INITIAL_UID = #{uid}")
    List<Check> showMyCheck(Integer uid);

    @Select("select * from [dbo].[Check] where CHECKID = #{checkid}")
    List<Check> selectCheckByCheckid(Integer checkid);

    @Select("select COMPLETETIME from [dbo].[Check] where TRUE_ID = #{rid}")
    List<String> selectCompletetimeById(String rid);

    @Select("select COMPLETETIME from [dbo].[Check] where CHECKID = #{checkid}")
    String selectCompletetimeByCheckid(Integer checkid);

    @Select("select COMPLETETIME from [dbo].[Check]")
    List<String> selectCompletetime();

    @Select("select * from [dbo].[Check] where TRUE_ID = #{rid} AND CHECKID = #{checkid} AND CHANGE = 1")
    Check selectCheckByCheckidRid(String rid,Integer checkid);

    //用于判断是否盘点过
    @Select("select ERROR from [dbo].[Check] where TRUE_ID = #{rid} AND CHECKID = #{checkid} AND CHANGE = 1")
    String selectErrorByCheckidRid(String rid,Integer checkid);

    @Insert("insert into [dbo].[Check] (INITIAL_UID,TRUE_ID) select UID,RNUM from [dbo].[Resource] where UID is not null AND RNUM = #{rid}")
    Boolean startCheck(String rid);

    @Insert("insert into [dbo].[Check] (CHECKID,INITIAL_UID,TRUE_ID,COMPLETETIME,ERROR,TIPS,CHANGE) values " +
            "(#{checkid},#{initial_uid},#{trueid},#{completetime},#{error},#{tips},2)")
    Boolean refreshCheck(Integer checkid,Integer initial_uid,String trueid,String completetime,String error,String tips);

    //盘点的时候插入扫描到的资产（因为存在未登记的资产的关系）
    @Insert("insert into [dbo].[Check] (CHECKID,INITIAL_UID,TRUE_ID,COMPLETETIME,ERROR,TIPS,CHANGE) values"+
            "(#{checkid},#{initial_uid},#{trueid},#{completetime},#{error},#{tips},1)")
    Boolean insertNoResourceError(Integer checkid,Integer initial_uid,String trueid,String completetime,String error,String tips);

    @Update("update [dbo].[Check] set ERROR = #{error},tips = #{tips} where CHECKID = #{checkid} AND INITIAL_UID = #{initial_uid} AND TRUE_ID = #{trueid} AND CHANGE = 2")
    Boolean updateNoResourceError(Integer checkid,Integer initial_uid,String trueid,String error,String tips);

    @Update("update [dbo].[Check] set COMPLETETIME=#{completeTime},CHECKID=#{checkid},CHANGE=#{change},ERROR='未盘点' where COMPLETETIME is null AND CHECKID is null")
    Boolean startTime(String completeTime, Integer checkid,Integer change);

    @Update("update [dbo].[Check] set ERROR = #{error},TIPS = #{tips} where TRUE_ID = #{rid} AND COMPLETETIME = #{lasttime} ")//AND CHANGE != 2
    Boolean hasError(String error,String tips,String rid,String lasttime);

    //管理员端盘点的时候只更新error的情况下用到
    @Update("update [dbo].[Check] set ERROR= #{error} where CHECKID = #{checkid} AND TRUE_ID = #{rid} AND CHANGE != 2")
    Boolean updateError(String error,Integer checkid,String rid);

    @Update("update [dbo].[Check] set CHECKID = #{checkid} where TRUE_ID = #{rid}")
    Boolean setCheckId(Integer checkid,Integer rid);

    @Delete("delete from [dbo].[Check] where CHECKID = #{checkid}")
    Boolean deleteCheckByCheckid(Integer checkid);

    //删除Change为2的数据，然后再添加一条数据。用于在重复盘点数据不一致的情况
    @Delete("delete from [dbo].[Check] where TRUE_ID = #{rid} AND CHECKID = #{checkid} AND CHANGE = 2")
    Boolean deleteCheckChange2(String rid,Integer checkid);
}
